#include "main.h"

//////////////////////////////////////////////////////////////////////////
// Filters
//////////////////////////////////////////////////////////////////////////
int Filter_CurrentMap(char* current, char* filter) {
	char* m = strtok(filter, "; ");
	while (m != NULL) {
		if (strcmp(m, current) == 0)
			return 1;
		m = strtok(NULL, "; ");
	}
	return 0;
}

//////////////////////////////////////////////////////////////////////////
// PopUps
//////////////////////////////////////////////////////////////////////////
POPUPDATAEX pud = {0};

LRESULT CALLBACK PopupPlayingWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
	switch (message) {
		case WM_COMMAND: {
			if (HIWORD(wParam) == STN_CLICKED) {
				CList_SetGlobalStatus(ID_STATUS_ONLINE, "");
				PUDeletePopUp(hWnd); 
				return 0;
			}
			break;
		}
		case WM_CONTEXTMENU: {
			CList_SetGlobalStatus(ID_STATUS_ONLINE, "");
			PUDeletePopUp(hWnd); 
			break;
		}
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}


LRESULT CALLBACK PopupWindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
	switch (message) {
		case WM_COMMAND: {
			if (HIWORD(wParam) == STN_CLICKED) {
				OnJoinServer((WPARAM)PUGetContact(hWnd), NULL);
				PUDeletePopUp(hWnd); 
				return 0;
			}
			break;
		}
		case WM_CONTEXTMENU: {
			PUDeletePopUp(hWnd); 
			break;
		}
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

int	PopUp_Playing(HANDLE hContact, char* contactName, char* message) {
	ZeroMemory(&pud, sizeof(POPUPDATAEX));
	pud.lchContact = hContact;
	pud.lchIcon = LoadIconEx(IDI_MAIN);
	pud.PluginWindowProc = PopupPlayingWindowProc;
	strcpy(pud.lpzContactName, contactName);
	strcpy(pud.lpzText, message);
	pud.iSeconds = -1;
	return PUAddPopUpEx(&pud);
}


int	Notification_Map(HANDLE hContact, char* contactName, GS_SERVERINFO* info) {
	char message[MAX_SECONDLINE];
	ZeroMemory(&pud, sizeof(POPUPDATAEX));
	ZeroMemory(message, sizeof(message));
	sprintf(message, "Current Map: %s. Limits: %d/%d", info->map, info->timelimit, info->scorelimit);
	pud.lchContact = hContact;
	pud.lchIcon = LoadIconEx(IDI_MAIN);
	pud.PluginWindowProc = PopupWindowProc;
	strcpy(pud.lpzContactName, contactName);
	strcpy(pud.lpzText, message);
	pud.iSeconds = -1;
	SkinPlaySound(options.pszMapOnlineSound);
	return PUAddPopUpEx(&pud);
}

//////////////////////////////////////////////////////////////////////////
// Update Timer
//////////////////////////////////////////////////////////////////////////
uintptr_t timer = 0;

VOID CALLBACK Timer_Proc() {
	CList_ForEachContact(CList_Update);
}

void Timer_Start(int interval) {
	if (timer) {
		Timer_Stop();
	}
	timer = SetTimer(NULL, 0, interval * 1000, (TIMERPROC)Timer_Proc);
}

void Timer_Stop() {
	KillTimer(NULL, timer);
	timer = 0;
}


//////////////////////////////////////////////////////////////////////////
// Icons
//////////////////////////////////////////////////////////////////////////
struct {
	char*  szDescr;
	char*  szName;
	int    iSize;
	int    defIconID;
	HANDLE hIconLibItem;
} static iconList[] = {
	{ LPGEN("Main"),		"main",		16,		IDI_MAIN	},
	{ LPGEN("Filter"),		"filter",	16,		IDI_FILTER	},
	{ LPGEN("Connect"),		"connect",	16,		IDI_CONNECT }
};


void AddIcons(void) {
	char szFile[MAX_PATH];
	GetModuleFileNameA(g_hInstance, szFile, MAX_PATH);

	SKINICONDESC sid = {0};
	sid.cbSize = sizeof(SKINICONDESC);
	sid.pszSection = PROTONAME;
	sid.pszDefaultFile = szFile;

	// add them one by one
	for (int i = 0; i < SIZEOF(iconList); i++) {
		char szTemp[255];
		mir_snprintf(szTemp, sizeof(szTemp), "%s_%s", PROTONAME, iconList[i].szName );
		sid.pszName = szTemp;
		sid.pszDescription = iconList[i].szDescr;
		sid.iDefaultIndex = -iconList[i].defIconID;
		sid.cx = sid.cy = iconList[i].iSize;
		iconList[i].hIconLibItem = (HANDLE)CallService(MS_SKIN2_ADDICON, 0, (LPARAM)&sid);
	}
}


HICON LoadIconEx(int iconId) {
	for (int i = 0; i < SIZEOF(iconList); i++)
		if (iconList[i].defIconID == iconId)
			return (HICON)CallService(MS_SKIN2_GETICONBYHANDLE, 0, (LPARAM)iconList[i].hIconLibItem);
	return NULL;
}


HANDLE GetIconHandle(int iconId) {
	for (int i=0; i < SIZEOF(iconList); i++)
		if (iconList[i].defIconID == iconId)
			return iconList[i].hIconLibItem;
	return NULL;
}

// ripped from gamequery
char *StatusModeToDbSetting(int status,const char *suffix) {
	char *prefix;
	static char str[64];

	switch (status) {
		case ID_STATUS_AWAY: prefix="Away";	break;
		case ID_STATUS_NA: prefix="Na";	break;
		case ID_STATUS_DND: prefix="Dnd"; break;
		case ID_STATUS_OCCUPIED: prefix="Occupied"; break;
		case ID_STATUS_FREECHAT: prefix="FreeChat"; break;
		case ID_STATUS_ONLINE: prefix="On"; break;
		case ID_STATUS_OFFLINE: prefix="Off"; break;
		case ID_STATUS_INVISIBLE: prefix="Inv"; break;
		case ID_STATUS_ONTHEPHONE: prefix="Otp"; break;
		case ID_STATUS_OUTTOLUNCH: prefix="Otl"; break;
		default: return NULL;
	}
	lstrcpy(str, prefix);
	lstrcat(str, suffix);
	return str;
}
